package com.ljb.main.v;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.hikvision.vmsnetsdk.CameraInfo;
import com.ljb.haikanglibrary.HaiKangManager;
import com.ljb.haikanglibrary.callbacks.MsgCallback;
import com.ljb.haikanglibrary.utils.L;
import com.ljb.haikanglibrary.utils.ThreadUtils;
import com.ljb.main.DataTemp;
import com.ljb.main.NoPreloadViewPager;
import com.ljb.main.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljb on 2017/6/1.
 */

public class MonitorActivity extends FragmentActivity {

    private NoPreloadViewPager vp;
    private List<CameraInfo> cameraInfoList;
    private List<Fragment> fragmentList;
    private PagerAdapter adapter;
    private int count;
    private int regionID;

    public static void actionStart(Context c, int regionID) {
        Intent intent = new Intent(c, MonitorActivity.class);
        intent.putExtra("regionID", regionID);
        c.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live);
        vp = (NoPreloadViewPager) findViewById(R.id.vp);
        getData();
    }

    private void getData() {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                regionID = getIntent().getIntExtra("regionID", 0);
                if (regionID > 0) {
                    HaiKangManager.init().requestSubResFromRegion(regionID, new MsgCallback() {
                        @Override
                        public void onMsg(int msgId, Object data) {
                            DataTemp.init().cameraInfoList_region = (List<CameraInfo>) data;
                            initData();
                        }
                    });

                } else {
                    DataTemp.init().cameraInfoList_region = DataTemp.init().cameraInfoList_my;
                    initData();
                }
            }
        });
    }

    private void initUI() {
        ThreadUtils.runUIThread(new Runnable() {
            @Override
            public void run() {
                adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
                    @Override
                    public int getCount() {
                        return count;
                    }

                    @Override
                    public Fragment getItem(int position) {
                        L.e("获取第" + position + "个条目");
                        return fragmentList.get(position);
                    }
                };
                vp.setAdapter(adapter);
            }
        });
    }


    private void initData() {
        cameraInfoList = DataTemp.init().cameraInfoList_region;
        fragmentList = new ArrayList<>();
        int size = cameraInfoList.size();

        if (size % 6 == 0) {
            count = size / 6;
        } else {
            count = size / 6 + 1;
        }

        if (count == 0) {
            count = 1;
        }

        L.e("监控点总数为:" + size + "一共需要显示" + count + "页");

        for (int i = 0; i < count; i++) {
            fragmentList.add(LiveFragment.newInstance(i));
        }

        initUI();
    }
}

